<!DOCTYPE html>
<html>
<head>   
    <title>menus</title>
    <script type="text/javascript" src="../javascript/page/Child.js"></script>
    <style type="text/css">
        html, body {
            font-size: 12px;  background-color: #f1f1f1;
        }
    </style>
</head>
<body onload="docLoaded();">
    <div id="queryForm" class="title_bar">
        <template>
            <div class="title_bar">
                <el-page-header @back="returnToParent" :content="parentMenuName"></el-page-header>
            </div>  
            <div class="query_form">
                <el-form :inline="true" :model="searchData" class="demo-form-inline">                    
                    <el-form-item label="MenuName">
                        <el-input v-model="searchData.menuName" placeholder="Menu name"></el-input>
                    </el-form-item>        
                    <el-form-item>
                        <el-button type="primary" @click="querySubmit">Query</el-button>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="handleAdd" icon="el-icon-plus" plain>Add</el-button>
                    </el-form-item>
                </el-form>
            </div>       
            <div class="hline"></div>
            <div class="panel">              
                <el-table :data="tableData" style="width: 100%" max-height="660">
                    <el-table-column prop="id" label="ID" min-width="5%"></el-table-column>
                    <el-table-column prop="menuName" label="Menu Name" min-width="12%"></el-table-column>
                    <el-table-column prop="menuPath" label="Menu Path" min-width="23%"></el-table-column>
                    <el-table-column prop="menuIco" label="Menu Ico" min-width="15%"></el-table-column>
                    <el-table-column prop="sortIndex" label="Sort" min-width="10%"></el-table-column>                    
                    <el-table-column prop="statusName" label="Status" min-width="10%"></el-table-column>
                    <el-table-column label="Operation" min-width="25%">
                        <template slot-scope="scope">
                            <el-button @click="handleClick(scope.row)" type="text" size="small" v-if="scope.row.isLeaf === 0">详情</el-button>              
                            <el-button @click="handleEdit(scope.row)" type="text" size="small">编辑</el-button>
                            <el-button @click="handleMove(scope.row)" type="text" size="small">移动</el-button>
                        </template>
                    </el-table-column>
                </el-table>                 
            </div>   
            <div class="status_bar" style="text-align: left;">
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="searchData.pageNum"
                    :page-sizes="[5, 10, 15]"
                    :page-size="searchData.pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="pageInfo.total">
                </el-pagination> 
            </div>
            <el-dialog :title="dialogTitle" :visible.sync="editDialogFormVisible"> 
                <el-form label-position="right" :inline="true" :model="dialogForm" :label-width="formLabelWidth">  
                    <el-form-item label="Parent">
                        <el-input v-model="dialogForm.parentName" disabled></el-input>
                    </el-form-item>                  
                    <el-form-item label="Menu Name">
                        <el-input v-model="dialogForm.menuName" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="Menu Path">
                        <el-input v-model="dialogForm.menuPath" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="Menu Ico">
                        <el-input v-model="dialogForm.menuIco" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="Sort">
                        <el-input v-model="dialogForm.sortIndex" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="Status">
                        <el-select v-model="dialogForm.status" placeholder="please select status">
                            <el-option v-for="item in statusList"
                            :key="item.status"
                            :label="item.name"
                            :value="item.status">
                            </el-option>
                        </el-select>
                    </el-form-item>                  
                    <el-form-item>
                        <el-checkbox v-model="dialogForm.leafCheck" border>Leaf without child nodes</el-checkbox>
                    </el-form-item>  
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="editDialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="editSubmit">确 定</el-button>
                </div>                
              </el-dialog>
              <el-dialog title="Move Menu" :visible.sync="moveDialogFormVisible"> 
                <el-form :model="dialogForm"> 
                    <el-form-item label="Menu" :label-width="formLabelWidth">
                        <el-input v-model="moveDialogForm.menuName" disabled></el-input>
                    </el-form-item>                   
                    <el-form-item label="Parent" :label-width="formLabelWidth">
                        <el-cascader
                        v-model="menuData.values"
                        :options="menuData.options"
                        :props="menuData.props">
                        </el-cascader>                      
                    </el-form-item>  
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="moveDialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="moveSubmit">确 定</el-button>
                </div>                
              </el-dialog>
        </template>
    </div>
    <script type="text/javascript">
        var router = startPage();
        function docLoaded() {           
            var queryApp = new Vue({el: '#queryForm', data: {     
                    parentChain: [],                
                    parentMenuId: 0,
                    parentMenuName: "root",            
                    searchData:{                        
                        parentId: 0,
                        menuName: "", 
                        pageNum:1,
                        pageSize:5
                    },
                    pageInfo:{
                        totalPage:0,
                        total:0
                    },
                    tableData:[],
                    statusList:[{status:0,name:"normal"},{status:1,name:"disable"}],
                    formLabelWidth: '120px',
                    editDialogFormVisible:false,                    
                    dialogTitle: "Edit",
                    dialogMode: 0,
                    dialogForm:{                      
                        id:0,
                        menuName:"",
                        menuPath:"", 
                        menuIco:"", 
                        parentId: 0, 
                        sortIndex: 0,
                        isLeaf:0,
                        parentName: "root",        
                        status: 0,
                        leafCheck:false
                    },
                    moveDialogFormVisible: false,
                    moveDialogForm:{                      
                        id:0,
                        menuName:"",                      
                        parentId: 0,                        
                        parentName: "root"
                    },
                    menuData:{  
                        queryData:{
                            showDisable:true,
                            disableId:this.parentMenuId
                        },   
                        values:[],  
                        menuList: [],                
                        options: [],
                        props: {
                            expandTrigger: 'hover',
                            checkStrictly: true,
                            emitPath: true,
                            children: 'menuList',
                            label: 'menuName',
                            value: 'id'                                                     
                        }                                             
                    },             
                },
                methods: {
                    returnToParent() { 
                        if(this.parentMenuId == 0) return;      
                        var parentId = this.parentMenuId;
                        this.parentMenuId = 0;
                        this.parentMenuName = "root";                                     
                        while(this.parentChain.length > 0) {
                            var val = this.parentChain.pop();                       
                            if(val.id != parentId){
                                this.parentMenuId = val.id;
                                this.parentMenuName = val.name;    
                                break;
                            }                                     
                        }                                           
                        this.querySubmit();
                    },
                    loadMenuList() {                                
                        httpPost(apiMenuTree(this,router.authInfo,JSON.stringify(this.menuData.queryData))).success(function(res,context){ 
                            var menu = checkAndGetData(res);
                            if(!menu) return;
                            context.menuData.menuList = menu.menuList;        
                        }).then(function(status, res, context){context.$message.error(res);});
                    },            
                    querySubmit:function(event){
                        this.searchData.pageNum = 1;
                        this.loadTableData(event);
                    },        
                    loadTableData:function(event){
                        this.searchData.parentId = this.parentMenuId;                                            
                        httpPost(apiMenuPage(this,router.authInfo,JSON.stringify(this.searchData))).success(function(res,context){ 
                            parseAndCheckResult(context,res);
                        }).then(function(status, res, context){context.$message.error(res);});
                    },
                    handleSizeChange(val) {                     
                        this.searchData.pageSize = val;
                        this.loadTableData();
                    },
                    handleCurrentChange(val) {
                        this.searchData.pageNum = val;
                        this.loadTableData();
                    },
                    handleClick(val){               
                        if(val.isLeaf == 1){
                            return;
                        }  
                        this.parentMenuId = val.id;
                        this.parentMenuName = val.menuName;
                        this.parentChain.push({id:val.id,name:val.menuName});   
                        this.loadTableData();
                    },
                    handleEdit(row){
                        this.dialogForm = {
                            id:row.id, 
                            menuName:row.menuName, 
                            menuPath:row.menuPath, 
                            menuIco:row.menuIco, 
                            sortIndex: row.sortIndex,
                            isLeaf: row.isLeaf,
                            parentId: this.parentMenuId,
                            parentName: this.parentMenuName,    
                            status:row.status,
                            leafCheck:(row.isLeaf == 1)
                        };                       
                        this.dialogTitle = "Update Menu";
                        this.dialogMode = 0;
                        this.editDialogFormVisible = true;
                    },                      
                    handleMove(row){
                        this.moveDialogForm.id = row.id;
                        this.moveDialogForm.menuName = row.menuName;
                        this.moveDialogForm.parentId = this.parentMenuId;
                        this.moveDialogForm.parentName = this.parentMenuName;
                        this.menuData.values = [this.parentMenuId];
                        this.menuData.options = [{
                            id:0,
                            menuName:'root',
                            isLeaf:0,
                            menuList:this.menuData.menuList.slice()
                        }]; 
                        for(var m of this.menuData.options){                           
                            m.disabled = false;   
                            if(m.id == row.id || m.isLeaf == 1){
                                m.disabled = true;                               
                            }  
                            if(m.menuList == null || m.menuList.length == 0){
                                delete m.menuList;
                                continue;
                            }                  
                            this.disableOptions(m.menuList,row.id,m.disabled);                               
                        }                       
                        this.moveDialogFormVisible = true;
                    },
                    disableOptions(options,id,parentDisable){
                        for(var m of options){                                                    
                            m.disabled = parentDisable;
                            if(m.id == id || m.isLeaf == 1){
                                m.disabled = true;                               
                            }   
                            if(m.menuList == null || m.menuList.length == 0){
                                delete m.menuList;
                                continue;
                            }        
                            this.disableOptions(m.menuList,id,m.disabled);
                        }                
                    },
                    moveSubmit(event){ 
                        this.moveDialogFormVisible = false;
                        if(this.menuData.values == null || !Array.isArray(this.menuData.values)){
                            return;
                        }    
                        var moveToParentId = this.menuData.values[this.menuData.values.length-1];
                        if(this.moveDialogForm.id == moveToParentId) {
                            return;
                        }              
                        this.moveDialogForm.parentId = moveToParentId;
                        this.moveDialogForm.parentName = '';
                        var apiRequest = apiMenuMove(this,router.authInfo,JSON.stringify(this.moveDialogForm));
                        httpPost(apiRequest).success(function(res,context){                          
                            checkResult(context,res,false);                           
                            context.querySubmit();
                        }).then(function(status, res, context){checkResult(context,res,true);});                      
                    },
                    handleAdd(){
                        this.dialogForm = {
                            id:0,
                            menuName:"",
                            menuPath:"", 
                            menuIco:"", 
                            sortIndex: 0,
                            parentId: this.parentMenuId,       
                            parentName: this.parentMenuName,                                                   
                            status:0
                        };                           
                        this.dialogTitle = "Add New Menu";
                        this.dialogMode = 1;
                        this.editDialogFormVisible = true;
                    },
                    editSubmit:function(event){
                        this.editDialogFormVisible = false;
                        this.dialogForm.parentId = this.parentMenuId;
                        this.dialogForm.isLeaf = this.dialogForm.leafCheck?1:0;
                        var apiRequest = (this.dialogMode)?apiMenuAdd(this,router.authInfo,JSON.stringify(this.dialogForm)):apiMenuUpdate(this,router.authInfo,JSON.stringify(this.dialogForm));
                        httpPost(apiRequest).success(function(res,context){                          
                            checkResult(context,res,false);                           
                            context.loadTableData();
                        }).then(function(status, res, context){checkResult(context,res,true);});
                    }
                }
            }); 
            queryApp.querySubmit();
            queryApp.loadMenuList();//init end.
        } 

        function parseAndCheckResult(vApp, res){
            var resObj = JSON.parse(res); 
            if(resObj.code != SUCCESS_CODE){
                return false;
            }
            if(resObj.pageInfo){
                vApp.pageInfo.total = resObj.pageInfo.total;
                vApp.pageInfo.totalPage = resObj.pageInfo.totalPage;
            }
            if(resObj.data){                
                var arrayObj = new Array();
                for(var item of resObj.data){
                    arrayObj.push({
                        id:item.id,
                        menuName:item.menuName,
                        menuPath:item.menuPath, 
                        menuIco:item.menuIco, 
                        parentId:item.parentId, 
                        sortIndex:item.sortIndex,  
                        isLeaf:item.isLeaf,                                         
                        status:item.deleted,
                        statusName:(item.deleted==1)?"disable":"normal"});
                }
                vApp.tableData = arrayObj;
            }
            return resObj;
        }

        function checkResult(vApp, res, isErr){            
            if(isErr){
                vApp.$message.error(res);
                return;
            }
            var resObj = JSON.parse(res); 
            if(resObj.code == SUCCESS_CODE){
                vApp.$message({type: 'success', message: resObj.message});
                return;
            }
            vApp.$message.error(resObj.message);
        }

        function checkAndGetData(res){            
            var resObj = JSON.parse(res); 
            if(resObj.code != SUCCESS_CODE){
                return false;
            }
            return resObj.data;
        }
    </script>
</body>
</html>
